package com.taozi.netty.socketio.use.classroomchat.service.socketio;


import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import com.taozi.netty.socketio.use.classroomchat.constant.EducationConstant;
import com.taozi.netty.socketio.use.classroomchat.entity.entity.education.ClassroomUser;
import com.taozi.netty.socketio.use.classroomchat.mapper.education.ClassroomUserMapper;
import com.taozi.netty.socketio.use.classroomchat.entity.vo.education.ClassroomChatVo;
import com.taozi.netty.socketio.use.classroomchat.entity.vo.education.ChatMessageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * description 课堂聊天室连接状态监听
 *
 * @author mingtao
 * date：2019/7/1 16:33
 */
@Service("eventListenner")
@Slf4j
public class EventListenner {

    @Autowired
    ClassroomUserMapper classroomUserMapper;

    SocketIOServer socketIOServer;
    public void setServer(SocketIOServer server) {
        this.socketIOServer = server;
    }

    // 连接时调用
    @OnConnect
    public void onConnect(SocketIOClient client) {
        log.info("连接哟~");
    }

    // 连接关闭时调用
    @OnDisconnect
    public void onDisconnect(SocketIOClient client) {

        log.info("关闭啦~");
        Map<String, List<String>> mapParams = client.getHandshakeData().getUrlParams();// 从请求的连接中拿出参数
        List<String> classroomIdList = mapParams.get("classroomId");
        List<String> userIdList = mapParams.get("userId");
        if(classroomIdList == null || userIdList == null || classroomIdList.size() <= 0 || userIdList.size() <= 0){
            return;
        }
        String classroomId = classroomIdList.get(0);
        String userId = userIdList.get(0);
        updateClassroomUser(Integer.parseInt(classroomId), Integer.parseInt(userId));

        //根据课堂id查询课堂在线人员
        ClassroomChatVo classroomChatVo = new ClassroomChatVo();
        classroomChatVo.setIsOnline(EducationConstant.STATUS_YES);
        classroomChatVo.setClassroomId(Integer.parseInt(classroomId));
        List<ClassroomUser> classroomUserList = classroomUserMapper.selectByClassroomId(classroomChatVo);

        //发送新的在线人员列表
        socketIOServer.getBroadcastOperations().sendEvent("onlineUser" + classroomId, classroomUserList);
    }

    //私聊时调用
    @OnEvent(value = "oneToOne")
    public void onOneToOneEvent(SocketIOClient client, ChatMessageVo chatMessageVo, AckRequest ackRequest){
        if(!client.isChannelOpen()){
            if(ackRequest.isAckRequested()){
                ackRequest.sendAckData("连接已断开，请重新连接");
            }
            return;
        }
        if(chatMessageVo == null || chatMessageVo.getChatType() == null
                || chatMessageVo.getFromId() == null || chatMessageVo.getToId() == null
                || chatMessageVo.getMsgType() == null || chatMessageVo.getMsgContent() == null
                || chatMessageVo.getChatType() == null){
            if(ackRequest.isAckRequested()){
                ackRequest.sendAckData("参数缺失");
            }
            return;
        }
        if(chatMessageVo.getChatType() != 2){
            if(ackRequest.isAckRequested()){
                ackRequest.sendAckData("不是私聊消息");
            }
            return;
        }
        //发送私聊消息
        client.sendEvent("oneToOne" + chatMessageVo.getToId(), chatMessageVo);


    }

    //群聊时调用
    @OnEvent(value = "oneToMany")
    public void onOneToManyEvent(SocketIOClient client, ChatMessageVo chatMessageVo, AckRequest ackRequest){
        if(!client.isChannelOpen()){
            if(ackRequest.isAckRequested()){
                ackRequest.sendAckData("连接已断开，请重新连接");
            }
            return;
        }
        if(chatMessageVo == null || chatMessageVo.getGroupId() == null
                || chatMessageVo.getFromId() == null || chatMessageVo.getMsgType() == null
                || chatMessageVo.getMsgContent() == null || chatMessageVo.getChatType() == null){
            if(ackRequest.isAckRequested()){
                ackRequest.sendAckData("参数缺失");
            }
            return;
        }
        if(chatMessageVo.getChatType() != 1){
            if(ackRequest.isAckRequested()){
                ackRequest.sendAckData("不是公聊消息");
            }
            return;
        }
        //发送公聊消息
        socketIOServer.getBroadcastOperations().sendEvent("oneToMany" + chatMessageVo.getGroupId(), chatMessageVo);

    }

    private void updateClassroomUser(Integer classroomId, Integer userId){

        if(classroomId == null || userId == null){
            return ;
        }

        //修改在线状态
        ClassroomUser classroomUser = new ClassroomUser();
        classroomUser.setUpdateTime(new Date());
        classroomUser.setIsOnline(EducationConstant.STATUS_NO);
        classroomUser.setClassroomId(classroomId);
        classroomUser.setUserId(userId);
        classroomUserMapper.updateByClassroomId(classroomUser);

    }


}
